import sys
from MainWindow import Ui_MainWindow
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtMultimedia import *

class MyMainWindow(QMainWindow):
    def __init__(self,parent=None):
        super().__init__(parent)
        self.ui=Ui_MainWindow()
        self.ui.setupUi(self)
        self.player=QMediaPlayer(self)
        self.playlist=QMediaPlaylist(self)
        self.player.setPlaylist(self.playlist)
        self.playlist.setPlaybackMode(QMediaPlaylist.Loop)#设置成循环模式
        self.__duration=""#文件总长度
        self.__curPos=""#当前播放的位置"
        self.player.stateChanged.connect(self.do_stateChanged)
        self.player.positionChanged.connect(self.do_postionChanged)
        self.player.durationChanged.connect(self.do_durationChanged)
        self.playlist.currentIndexChanged.connect(self.do_currentChanged)
    def closeEvent(self, event):
        if(self.player.state()==QMediaPlayer.PlayingState):
            self.player.stop()
    @pyqtSlot()
    def on_btnAdd_clicked(self):
        curPath=QDir.currentPath()
        dlgTitle="选择音频文件"
        filt="音频文件(*.mp3 *wav *wma);;所有文件(*.*)"
        fileList,flt=QFileDialog.getOpenFileNames(self,dlgTitle,curPath,filt)
        count=len(fileList)
        if count<1:
            return
        filename=fileList[0]
        fileInfo=QFileInfo(filename)
        QDir.setCurrent(fileInfo.absolutePath())#重设当前的路径
        for i in  range(count):
            filename=fileList[i]
            fileInfo.setFile(filename)
            song=QMediaContent(QUrl.fromLocalFile(filename))
            self.playlist.addMedia(song)#添加播放媒体
            basename=fileInfo.baseName()
            self.ui.listWidget.addItem(basename)#添加到界面文件列表
        if (self.player.state()!=QMediaPlayer.PlayingState):
            self.playlist.setCurrentIndex(0)
            self.player.play()

    @pyqtSlot()
    def on_btnRemove_clicked(self):
        pos=self.ui.listWidget.currentRow()
        item=self.ui.listWidget.takeItem(pos)
        if (self.playlist.currentIndex()==pos):#是我们当前播放的曲目
            nextPos=0
            if pos>=1:
                nextPos=pos-1
            self.playlist.removeMedia(pos)#从播放列表中删除
            if self.ui.listWidget.count()>0:#剩余的个数
                self.playlist.setCurrentIndex(nextPos)
                self.do_currentChanged(nextPos)  #当前曲目变化
            else:
                self.player.stop()
                self.ui.LabCurMedia.setText("无曲目")
        else:
            self.playlist.removeMedia(pos)
    @pyqtSlot()
    def on_btnClear_clicked(self):
        self.playlist.clear()
        self.ui.listWidget.clear()
        self.player.stop()
    #双击切换曲目
    def on_listWidget_doubleClicked(self,index):
        rowno=index.row()
        self.playlist.setCurrentIndex(rowno)
        self.player.play()
    @pyqtSlot()
    def on_btnPlay_clicked(self):
        if (self.playlist.currentIndex()<0):
            self.playlist.setCurrentIndex(0)
        self.player.play()
    @pyqtSlot()
    def on_btnPause_clicked(self):
        self.player.pause()

    @pyqtSlot()
    def on_btnStop_clicked(self):
        self.player.stop()
    @pyqtSlot()
    def on_btnPrevious_clicked(self):
        self.playlist.previous()

    @pyqtSlot()
    def on_btnNex_clicked(self):
        self.playlist.next()

    @pyqtSlot()
    def on_btnSound_clicked(self):
        mute=self.player.isMuted()
        self.player.setMuted(not mute)
        if (mute):
            self.ui.btnSound.setIcon(QIcon(":/icons/images/volumn.bmp"))
        else:
            self.ui.btnSound.setIcon(QIcon(":/icons/images/mute.bmp"))
    #调节音量
    @pyqtSlot(int)
    def on_sliderVolumn_valueChanged(self,value):
        self.player.setVolume(value)

    #控制文件的进度
    @pyqtSlot(int)
    def on_sliderPosition_valueChanged(self,value):
        self.player.setPosition(value)

    #播放器状态控制
    def do_stateChanged(self,state):
        self.ui.btnPlay.setEnabled(state!=QMediaPlayer.PlayingState)
        self.ui.btnPause.setEnabled(state == QMediaPlayer.PlayingState)
        self.ui.btnStop.setEnabled(state == QMediaPlayer.PlayingState)
    #文件位置变化更新进度
    def do_postionChanged(self,postion):
        if (self.ui.sliderPosition.isSliderDown()):
            return
        self.ui.sliderPosition.setSliderPosition(postion)
        secs=postion/1000
        mins=secs/60
        secs=secs % 60
        self.__curPos="%d:%d" %(mins,secs)
        self.ui.LabRatio.setText(self.__curPos+"/"+self.__duration)

    def do_durationChanged(self,duration):
        self.ui.sliderPosition.setMaximum(duration)
        secs = duration / 1000
        mins = secs / 60
        secs = secs % 60
        self.__duration = "%d:%d" % (mins, secs)
        self.ui.LabRatio.setText(self.__curPos + "/" + self.__duration)
    def do_currentChanged(self,position):
        self.ui.listWidget.setCurrentRow(position)
        item=self.ui.listWidget.currentItem()
        if (item!=None):
            self.ui.LabCurMedia.setText(item.text())
if __name__ == '__main__':
    app=QApplication(sys.argv)
    win=MyMainWindow()
    win.show()
    sys.exit(app.exec_())